package com.easthome.controller;


import com.easthome.constant.MessageConstant;
import com.easthome.entity.PageResult;
import com.easthome.entity.QueryPageBean;
import com.easthome.pojo.CheckGroup;
import com.easthome.result.Result;
import com.easthome.service.CheckGroupService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/checkGroup")
public class CheckGroupController {
    @Autowired
    private CheckGroupService checkGroupService;

    /**
     * 查询所有检查组
     */
    @GetMapping("/queryAll")
    public Result queryAll() {
        List<CheckGroup> checkGroups = checkGroupService.queryAll();
        if (checkGroups != null) {
            //查询成功
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroups);
        }
        return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
    }

    /**
     * 编辑检查组
     */
    @PutMapping("/editCheckGroup")
    public  Result editCheckGroup(Integer[] checkItemIds,
                                  @RequestBody CheckGroup checkGroup){
        checkGroupService.editCheckGroup(checkItemIds, checkGroup);
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    /**
     * 根据检查组的id查询检查项数据
     */
    @GetMapping("/queryCheckItemByCheckGroupId")
    public Result queryCheckItemsByCheckGroupId(Integer id) {
        List<Integer> checkItemIds = checkGroupService.queryCheckItemsByCheckGroupId(id);
        if (checkItemIds != null) {
            //查询成功
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItemIds);
        }
        return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
    }

    /**
     * 检查组数据的主键查询
     */
    @GetMapping("/checkGroupById")
    public Result checkGroupById(Integer id) {
        CheckGroup checkGroup = checkGroupService.checkGroupById(id);
        if (checkGroup != null) {
            //查询成功
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroup);
        }
        return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
    }


    /**
     * 添加检查组
     * 接收请求参数；
     * 1.检查组的基本信息
     * 2.检擦项的数据（主键id）
     */
   @PostMapping("/addCheckGroup")
    public Result addCheckGroup(Integer[] checkItemIds,
                               @RequestBody CheckGroup checkGroup) {
       for (Integer checkItemId : checkItemIds) {
           System.out.println("checkItemId = " + checkItemId);
       }
       System.out.println("checkGroup = " + checkGroup);
       //调用业务层
        checkGroupService.addCheckGroup(checkItemIds, checkGroup);
       return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
   }

    /**
     * 检查组分页查询
     * 请求参数：分页查询条件
     */
    @PostMapping("/queryCheckGroupPage")
    public Result queryCheckGroupPage(@RequestBody QueryPageBean queryPageBean) {
        //调用业务层
        PageResult pageResult = checkGroupService.queryCheckGroupPage(queryPageBean);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, pageResult);
    }

}
